package com.gang.music.core.toolbox;

import com.gang.music.system.entity.Menu;
import com.smallchill.core.toolbox.Func;

import java.util.List;

public class MenuUtils {

	
	public static String getMenu(List<Menu> menuList,List<Integer> userMenus){
		
		StringBuilder sb = new StringBuilder();
		if(menuList != null && menuList.size() > 0){
			
			for(Menu menu : menuList){
				
				if(userMenus.contains(menu.getId())){
					int childNum = menu.getChildMenus().size();
					
					sb.append("<li>\n")
					.append("<a class=\"J_menuItem\" ");
					if(!Func.isEmpty(menu.getMenuUrl()) && childNum == 0){
						sb.append("href=\"/"+ menu.getMenuUrl() +"\"");
					}
					sb.append(">\n")
					.append("<i class=\""+menu.getMenuIcon()+"\"></i>\n");
					if(childNum > 0){
						sb.append("<span class=\"nav-label\">"+ menu.getMenuName() +"</span>\n");
					}else {
						sb.append(menu.getMenuName());
					}
					sb.append("</a>\n");
					if(childNum > 0){
						sb.append(getChildren(menu.getChildMenus(),userMenus));
					}
					sb.append("</li>\n");
				}
			}
		}
		
		return sb.toString();
	}
	
	public static String getChildren(List<Menu> menuList,List<Integer> userMenus){
		StringBuilder sb = new StringBuilder();
		
		sb.append("<ul class=\"nav nav-second-level\">\n");
		
		for(Menu menu : menuList){
			
			if(userMenus.contains(menu.getId())){
				int childNum = menu.getChildMenus().size();
				
				sb.append("<li>\n")
				.append("<a class=\"J_menuItem\" ");
				if(!Func.isEmpty(menu.getMenuUrl()) && childNum == 0){
					sb.append("href=\"/"+ menu.getMenuUrl() +"\"");
				}
				sb.append(">\n")
				.append("<i class=\""+menu.getMenuIcon()+"\"></i>\n");
				if(childNum > 0){
					sb.append("<span class=\"nav-label\">"+ menu.getMenuName() +"</span>\n");
				}else {
					sb.append(menu.getMenuName());
				}
				sb.append("</a>\n");
				if(childNum > 0){
					sb.append(getChildren(menu.getChildMenus(),userMenus));
				}
				sb.append("</li>\n");
			}
		}
		
		sb.append("</ul>");
		
		return sb.toString();
	}
}
